﻿using B.S.BaseData.ErrorCode;
using B.S.BaseData.Write.API.Application.Command;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.BaseData.Write.API.Controllers
{
    /// <summary>
    /// API控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class GoodsInfoController : ControllerBase
    {

        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="mediator"></param>
        public GoodsInfoController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns></returns>
        [HttpPost]
        public Task<ApiResult<object>> GoodsAdd(GoodsCreateCommand command)
        {
            return mediator.Send(command);
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpDelete]
        public Task<ApiResult<object>> GoodsDel([FromQuery]GoodsDeleteCommand command)
        {
            return mediator.Send(command);
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="command">命令</param>
        /// <returns>返回任务</returns>
        [HttpPut]
        public Task<ApiResult<object>> GoodsUpdate([FromBody]GoodsUpdateCommand command)
        {
            return mediator.Send(command);
        }

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Img(IFormFile file)
        {
            var path = Directory.GetCurrentDirectory() + @"\wwwroot\img" + file.FileName;
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                file.CopyTo(fs);
                fs.Flush();
            }

            return Ok("http://localhost:5243/img" + file.FileName);
        }
    }
}
